<template>
  <div class="edit-popup">
    <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
      <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
        <el-form-item label="名称" prop="name">
          <el-input v-model="formData.name" :disabled="isRoot" placeholder="请输入账号" clearable />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入名称" clearable />
        </el-form-item>
        <el-form-item label="联系人" prop="contactName">
          <el-input v-model="formData.contactName" placeholder="请输入名称" clearable />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="formData.contactPhone" placeholder="请输入名称" clearable />
        </el-form-item>
        <el-form-item label="业务员" prop="salesmanId">
          <el-select v-model="formData.salesmanId" filterable remote reserve-keyword placeholder="输入搜索"
            :remote-method="searchSalesmans" :loading="loading" style="width: 240px">
            <el-option v-for="item in salesmans" :key="item.id" :label="item.nickname" :value="item.id" />
          </el-select>

        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :value="1" size="large">正常</el-radio>
            <el-radio :value="0" size="large">停止</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { addApi, editApi, detailApi } from '@/api/merchant'
import feedback from '@/utils/feedback'
import { computed, reactive, ref, shallowRef } from 'vue'
import { listsApi as getSalesmanListsApi } from '@/api/salesman'
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑管理员' : '新增管理员'
})

const formData = reactive({
  id: '',
  name: '',
  address: '',
  contactName: '',
  contactPhone: '',
  salesman: '',
  salesmanId: '',
  status: 1,
})

const isRoot = computed(() => {
  return formData.id == '1'
})

const formRules = reactive({
  name: [
    {
      required: true,
      message: '请输入商家',
      trigger: ['blur'],
    },
  ],
  address: [
    {
      required: true,
      message: '请输入商家地址',
      trigger: ['blur'],
    },
  ],
  contactName: [
    {
      required: true,
      message: '请选联系人姓名',
      trigger: ['blur'],
    },
  ],
  contactPhone: [
    {
      required: true,
      message: '请输入联系人电话',
      trigger: ['blur'],
    },
  ],
  salesmanId: [
    {
      required: true,
      message: '请选择业务员',
      trigger: ['blur'],
    },
  ],
})
const loading = ref(false)
const salesmans = ref<any>([])
const searchSalesmans = (name: string) => {
  loading.value = true
  salesmans.value = []
  getSalesmanListsApi({
    name,
    pageSize: 10,
  })
    .then(data => {
      salesmans.value = data.lists
      loading.value = false
    })
    .catch(() => {
      loading.value = false
      salesmans.value = []
    })
}
const handleSubmit = async () => {
  await formRef.value?.validate()
  mode.value == 'edit' ? await editApi(formData) : await addApi(formData)
  popupRef.value?.close()
  feedback.msgSuccess('操作成功')
  emit('success')
}

const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
  searchSalesmans("")
}

const setFormData = async (row: any) => {
  const data = await detailApi({
    id: row.id,
  })
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const handleClose = () => {
  emit('close')
}

defineExpose({
  open,
  setFormData,
})
</script>
